package com.flyduck.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.common.util.IdUtil;
import com.flyduck.common.util.StringUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.common.vo.PageResult;
import com.flyduck.system.dao.SysRoleDao;
import com.flyduck.system.dao.SysRoleMenuDao;
import com.flyduck.system.enums.StatusEnum;
import com.flyduck.system.vo.role.*;
import com.flyduck.system.vo.role.trans.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flyduck.system.entity.SysRoleEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleService {


    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    /**
     * 获取角色下拉(未删除、启用)
     *
     * @return
     */
    public List<SysRoleSelectRespVO> getRoleSelect() {
        QueryWrapper<SysRoleEntity> wrapper = getRoleSelectWrapper();
        return Lists.transform(sysRoleDao.list(wrapper), new SysRoleEntiyToSysRoleSelectRespVO());

    }

    private QueryWrapper<SysRoleEntity> getRoleSelectWrapper() {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusEnum.ENABLE.getValue());
        return wrapper;
    }

    /**
     * 获取角色分页
     *
     * @param reqVo
     * @param page
     * @param size
     * @return
     */
    public PageResult<SysRoleRespVO> getRolePageList(SysRoleReqVO reqVo, Integer page, Integer size) {
        //************************* 1.构建分页条件 *************************
        Page<SysRoleEntity> roleEntityPage = new Page<>(page, size);
        //************************* 2.构建查询条件 *************************
        QueryWrapper<SysRoleEntity> wrapper = getRoleQueryWrapper(reqVo);
        //************************* 3.查询 *************************
        IPage<SysRoleEntity> roleEntityIPage = sysRoleDao.page(roleEntityPage, wrapper);
        //************************* 4.结果返回转换 *************************
        List<SysRoleRespVO> respVOList = Lists.transform(roleEntityIPage.getRecords(), new SysRoleEntityToSysRoleRespVO());
        return PageResult.ofPages(respVOList, roleEntityIPage);

    }

    private QueryWrapper<SysRoleEntity> getRoleQueryWrapper(SysRoleReqVO reqVo) {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getRoleName())) {
            wrapper.like("role_name", reqVo.getRoleName());
        }
        return wrapper;
    }

    /**
     * 获取角色详情
     *
     * @param id
     * @return
     */
    public SysRoleDetailRespVO getRoleDetail(Long id) {
        SysRoleEntity sysRoleEntity = sysRoleDao.getById(id);
        SysRoleDetailRespVO sysRoleDetailRespVO = new SysRoleEntityToSysRoleDetailRespVO().apply(sysRoleEntity);
        List<Long> menuIds = sysRoleMenuDao.getMenuIdsByRoleIds(Arrays.asList(id));
        sysRoleDetailRespVO.setMenuIds(menuIds.stream().map(x -> x + "").collect(Collectors.toList()));
        return sysRoleDetailRespVO;
    }

    /**
     * 新增角色
     *
     * @param reqVo
     * @return
     */
    @Transactional
    public boolean addRole(SysRoleAddReqVO reqVo) {
        SysRoleEntity sysRoleEntity = new SysRoleAddReqVOToSysRoleEntity().apply(reqVo);
        sysRoleEntity.setId(IdUtil.getId());
        sysRoleEntity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysRoleEntity.setCreateTime(new Date());
        sysRoleEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysRoleEntity.setUpdateTime(new Date());
        sysRoleDao.save(sysRoleEntity);
        //保存角色和菜单的关系
        List<Long> menuIds = reqVo.getMenuIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList());
        sysRoleMenuDao.insertRoleMenuList(sysRoleEntity.getId(), menuIds);
        return true;
    }

    /**
     * 修改角色
     *
     * @param reqVo
     * @return
     */
    @Transactional
    public boolean updateRole(SysRoleUpdateReqVO reqVo) {
        SysRoleEntity roleEntity = new SysRoleUpdateReqVOToSysRoleEntity().apply(reqVo);
        roleEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        roleEntity.setUpdateTime(new Date());
        //删除角色和菜单的关系
        sysRoleMenuDao.deleteByRoleId(roleEntity.getId());
        //保存角色和菜单的关系
        List<Long> menuIds = reqVo.getMenuIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList());
        sysRoleMenuDao.insertRoleMenuList(roleEntity.getId(), menuIds);
        return true;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Transactional
    public boolean deleteRole(Long id) {
        sysRoleDao.removeById(id);
        sysRoleMenuDao.deleteByRoleId(id);
        return true;
    }

}
